// locutor
// (c) 2009, Normen Müller, normen.mueller@gmail.com  
// $Id: scopes.scala 4 2009-11-06 19:59:06Z normen.mueller $
package locutor.scope

import java.net._
import java.io._

import scala.collection.mutable.HashSet

import scaup.Views._

/** Companion to <code>Scope</code> class.
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.1 */
object Scope {
  /** The empty map of this type */  
  def empty[A <% PartiallyOrdered[A]] = new Scope[A]
}

/** The class <code>Scope</code> implements a scope representation on partially ordered data.
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.1 */
class Scope[A <% PartiallyOrdered[A]] extends HashSet[A] {
  /** Add <code>elem</code> to the scope, if not already covered.
   * 
   * <p>Note: This method serves as syntatic sugar for the Java bridge.</p>
   * 
   * @param elem the element to be added
   * @return 	 if <code>elem</code> not already covered, the set itself with the element added;
   *			 otherwise the unchanged set. */
  def add(elem: A): this.type = this + elem
  
  /** Checks if this scope covers element <code>elem</code>.
   * 
   *  @param elem the element to check for coverage.
   *  @return     <code>true</code> iff <code>elem</code> is covered in this scope */
  def covers(elem: A): Boolean = elements exists (elem <= _)
  
  /** Add <code>elem</code> to the scope, if not already covered.
   * 
   * <p>Note: This method serves as syntatic sugar for the Java bridge.</p>
   * 
   * @param elem the element to be added
   * @return     if <code>elem</code> not already covered, the set itself with the element added;
   * 		     otherwise the unchanged set. */
  override def + (elem: A): this.type = {
    if (! (this covers elem))
      +=(elem)
    this
  }  
}
